package cn.bgs.serviceedu.controller;


import cn.bgs.serviceedu.entity.Collections;
import cn.bgs.serviceedu.service.CollectionsService;
import cn.bgs.serviceedu.service.DoctorService;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YiChen
 * @since 2021-11-04
 */
@Api(description = "收藏表")
@RestController
@RequestMapping("/serviceedu/collection")
public class CollectionsController {

    @Autowired
    private CollectionsService collectionService;
    @Autowired
    private DoctorService doctorService;

    /**
     * 根据用户id查询所有收藏医生的数量
     * @param customerId
     * @return
     */
    @ApiOperation(value = "根据用户id查询所有收藏医生的数量")
    @GetMapping("count/{customerId}")
    public R count(@PathVariable Integer customerId){
     return  R.ok().data("count",doctorService.findDoct(customerId).size());
    }

    /**
     * 收藏医生
     * @param customerId
     * @param doctorId
     * @return
     */
    @ApiOperation(value = "收藏医生")
    @GetMapping("addCollection/{customerId}/{doctorId}")
    public R addCollection(@PathVariable Integer customerId,@PathVariable Integer doctorId){
        try {
            boolean b = collectionService.selectId(customerId, doctorId);
            if (!b){
                Collections collection = new Collections();
                collection.setCustomerId(customerId);
                collection.setDoctorId(doctorId);
                collectionService.save(collection);
                return R.ok().message("收藏成功");
            }
            return R.error().message("不可以重复收藏");
        } catch (Exception e) {
            throw new CSException(20001,"收藏失败请重试");
        }
    }

    /**
     * 取消收藏
     * @param customerId
     * @param doctorId
     * @return
     */
    @ApiOperation(value = "取消收藏")
    @GetMapping("removeCollection/{customerId}/{doctorId}")
    public R removeCollection(@PathVariable Integer customerId,@PathVariable Integer doctorId){
        try {
            boolean b = collectionService.selectId(customerId, doctorId);
            if (b){
                collectionService.removeCollection(customerId,doctorId);
                return R.ok().message("取消成功");
            }
            return R.error().message("不可以重复取消哦");
        } catch (Exception e) {
            throw new CSException(20001,"取消收藏失败请重试");
        }
    }
}
